<?php
namespace plugins\member_login\controller;
use cmf\controller\ApiThirdAccessBaseController;//引用插件基类
use RongCloud\RongCloud;
use think\Db;
use plugins\member\model\MemberModel;
use plugins\member\interfaceLib\MemberClassify;

define("APPKEY", 'p5tvi9dspe834');
define('APPSECRET', 'b8fHKCCpFMNe0N');
/**
 * 账号密码控制器
 */
class ApiAccountController extends ApiThirdAccessBaseController
{
	public function regiest($param)
	{
		$data = $param['data'];
		//参数验证
		$check['login_name'] = ['name'=>'登录名'];
		$check['nick_name'] = ['name'=>'昵称'];
		$check['password'] = ['name'=>'登陆密码'];
		checkParam($check,$data);
		//查重
		$check = MemberModel::get(['login_name' => $data['login_name']]);
		if($check){
			return api_json_echo( false , '用户名已存在' );
		}
		//需要添加的参数
		$data['password'] = cmf_password($data['password']);
		$data['join_date'] = time();
		$data['join_ip'] = $this->request->ip();
		//默认头像
		$data['head_portrait'] = '/plugins/member/view/public/user.png';		
		$member = new MemberModel();
		$result =  $member->allowField( true )->save($data);
		if( !$result ){
			return api_json_echo( false , '注册失败' );
		}
		//uid的补充
	    $rand = str_pad($member->id,4,"0",STR_PAD_LEFT);   
		$member->uid = 'zy_'.$rand.mt_rand(1000,9999);
		$member->uid_aes = ( new MemberClassify )->aesSetUid($member->uid);
		$member->save();
		//返回前端的参数
		$return['token'] = $this->setIdentity($member->uid);
		$return['refresh_token'] = $this->setRefreshToken($member->uid);
		$return['aes'] = $member->uid_aes;
		return api_json_echo( true , '会员注册成功',$return );
	}
	//登陆
	public function login($param){
        require 'RongCloud/RongCloud.php';
		$data = $param['data'];
		//参数验证
		$check['login_name'] = ['name'=>'登录名'];
		$check['password'] = ['name'=>'登陆密码'];
		checkParam($check,$data);
		//验证登录
		$member = MemberModel::get(['phone'=> $data[ 'login_name' ],'password' => cmf_password($data[ 'password' ])] );

		if( !$member ){
			return api_json_echo( false , '账号或密码不正确,请检查' );
		}
		//检测状态
		if($member->status == 1){

            $diviceid = '';    //设备号
            $cid = '';    //设备号
            if(!empty($data['diviceid'])){
                $diviceid = $data['diviceid'];
            }
            if(!empty($data['cid'])){
                $cid = $data['cid'];
            }

			//修改登陆时间与ip
			$result = $member->save(['last_date' => time(),'last_ip' => $this->request->ip(),'diviceid'=>$diviceid,'cid'=>$cid]);
			if($result){
				//生成token
				$return['uid'] = $member->uid;
				$return['token'] = $this->setIdentity($member->uid);
				$return['refresh_token'] = $this->setRefreshToken($member->uid);
				$return['aes'] = $member->uid_aes;

                if(empty($member->token)){
                    $RongSDK = new RongCloud(APPKEY, APPSECRET);
                    $user = [
                        'id' => $member->uid,
                        'name' => $member->nick_name,//用户名称
                        'portrait' => ZY_APP_PATH.$member->head_portrait //用户头像
                    ];
                    $register = $RongSDK->getUser()->register($user);
                    var_dump($register);
                    if($register['code'] == 200){
                        Db::name('member')->where('uid',$member->uid)->update([
                            'token'=>$register['token']
                        ]);
                    }
                }

				return api_json_echo( true , '会员登录成功' , $return );
			}else{
				return api_json_echo( false , '会员登录失败，请检查账号密码' );
			}
		}else if($member->status == 2){
			return api_json_echo( false , '该用户被禁用，请重试' );
		}else if($member->status == 3){
		}
		return api_json_echo( false , '会员登录失败，请检查账号密码' );
	}
}